package main

import (
	"fmt"
	"github.com/andlabs/ui"
	_ "github.com/andlabs/ui/winmanifest"
	"ll_csv_con/ccore"
	"ll_csv_con/cutil"
)

var mainwin *ui.Window

func makeSxCsv() ui.Control {
	hbox := ui.NewHorizontalBox()
	hbox.SetPadded(true)

	vbox := ui.NewVerticalBox()
	vbox.SetPadded(true)
	hbox.Append(vbox, true)

	grid := ui.NewGrid()
	grid.SetPadded(true)
	vbox.Append(grid, false)

	button := ui.NewButton("打开文件")
	entry := ui.NewEntry()
	entry.SetReadOnly(true)
	button.OnClicked(func(*ui.Button) {
		filename := ui.OpenFile(mainwin)
		if filename == "" {
			filename = "(cancelled)"
		}
		entry.SetText(filename)

	})
	grid.Append(button,
		0, 0, 1, 1,
		false, ui.AlignFill, false, ui.AlignFill)
	grid.Append(entry,
		1, 0, 1, 1,
		true, ui.AlignFill, false, ui.AlignFill)

	button2 := ui.NewButton("保存路径")
	entry2 := ui.NewEntry()
	//entry2.SetReadOnly(true)
	button2.OnClicked(func(*ui.Button) {
		filename := ui.SaveFile(mainwin)
		if filename == "" {
			filename = "(cancelled)"
		}
		entry2.SetText(filename)
	})
	button.OnClicked(func(*ui.Button) {
		filename := ui.OpenFile(mainwin)
		if filename == "" {
			filename = "(cancelled)"
		}
		entry.SetText(filename)
		entry2.SetText(ccore.GetNewFilename(filename))
	})
	grid.Append(button2,
		0, 1, 1, 1,
		false, ui.AlignFill, false, ui.AlignFill)
	grid.Append(entry2,
		1, 1, 1, 1,
		true, ui.AlignFill, false, ui.AlignFill)

	//筛选条件
	lable11 := ui.NewButton("列名")
	entrylb11 := ui.NewEntry()
	grid.Append(lable11,
		0, 2, 1, 1,
		false, ui.AlignFill, false, ui.AlignFill)
	grid.Append(entrylb11,
		1, 2, 1, 1,
		true, ui.AlignFill, false, ui.AlignFill)
	lable12 := ui.NewButton("条件")
	entrylb12 := ui.NewEntry()
	grid.Append(lable12,
		0, 3, 1, 1,
		false, ui.AlignFill, false, ui.AlignFill)
	grid.Append(entrylb12,
		1, 3, 1, 1,
		true, ui.AlignFill, false, ui.AlignFill)

	msggrid := ui.NewGrid()
	msggrid.SetPadded(true)
	grid.Append(msggrid,
		0, 4, 4, 1,
		false, ui.AlignCenter, false, ui.AlignStart)

	button = ui.NewButton("执行")
	button.OnClicked(func(*ui.Button) {
		csv1path := entry.Text()
		target := entrylb11.Text()
		key := entrylb12.Text()
		icase := "yes"
		fmt.Println("原文件路径" + entry.Text())
		fmt.Println("保存文件路径" + entry2.Text())
		fmt.Println("列名" + entrylb11.Text())
		fmt.Println("搜索条件" + entrylb12.Text())
		if entry2.Text() != "" && entry2.Text() != "(cancelled)" {
			ccore.NewFileName = entry2.Text()
		}
		ccore.Sx(csv1path, target, key, icase)
		ui.MsgBox(mainwin,
			"执行",
			"执行成功")
	})
	msggrid.Append(button,
		0, 0, 1, 1,
		false, ui.AlignFill, false, ui.AlignFill)
	return hbox
}

func makeHbCsv() ui.Control {
	hbox := ui.NewHorizontalBox()
	hbox.SetPadded(true)

	vbox := ui.NewVerticalBox()
	vbox.SetPadded(true)
	hbox.Append(vbox, true)

	grid := ui.NewGrid()
	grid.SetPadded(true)
	vbox.Append(grid, false)

	button := ui.NewButton("打开主文件")
	entry := ui.NewEntry()
	entry.SetReadOnly(true)
	button.OnClicked(func(*ui.Button) {
		filename := ui.OpenFile(mainwin)
		if filename == "" {
			filename = "(cancelled)"
		}
		entry.SetText(filename)
	})
	grid.Append(button,
		0, 0, 1, 1,
		false, ui.AlignFill, false, ui.AlignFill)
	grid.Append(entry,
		1, 0, 1, 1,
		true, ui.AlignFill, false, ui.AlignFill)

	buttondb := ui.NewButton("打开对比文件")
	entrydb := ui.NewEntry()
	entrydb.SetReadOnly(true)
	buttondb.OnClicked(func(*ui.Button) {
		filename := ui.OpenFile(mainwin)
		if filename == "" {
			filename = "(cancelled)"
		}
		entrydb.SetText(filename)
	})
	grid.Append(buttondb,
		0, 1, 1, 1,
		false, ui.AlignFill, false, ui.AlignFill)
	grid.Append(entrydb,
		1, 1, 1, 1,
		true, ui.AlignFill, false, ui.AlignFill)

	button2 := ui.NewButton("保存路径")
	entry2 := ui.NewEntry()
	//entry2.SetReadOnly(true)
	button2.OnClicked(func(*ui.Button) {
		filename := ui.SaveFile(mainwin)
		if filename == "" {
			filename = "(cancelled)"
		}
		entry2.SetText(filename)
	})
	button.OnClicked(func(*ui.Button) {
		filename := ui.OpenFile(mainwin)
		if filename == "" {
			filename = "(cancelled)"
		}
		entry.SetText(filename)
		entry2.SetText(ccore.GetNewFilenameHb(filename))
	})
	grid.Append(button2,
		0, 2, 1, 1,
		false, ui.AlignFill, false, ui.AlignFill)
	grid.Append(entry2,
		1, 2, 1, 1,
		true, ui.AlignFill, false, ui.AlignFill)

	//筛选条件
	lable11 := ui.NewButton("文件1列名")
	entrylb11 := ui.NewEntry()
	grid.Append(lable11,
		0, 3, 1, 1,
		false, ui.AlignFill, false, ui.AlignFill)
	grid.Append(entrylb11,
		1, 3, 1, 1,
		true, ui.AlignFill, false, ui.AlignFill)
	lable12 := ui.NewButton("文件1列名")
	entrylb12 := ui.NewEntry()
	grid.Append(lable12,
		0, 4, 1, 1,
		false, ui.AlignFill, false, ui.AlignFill)
	grid.Append(entrylb12,
		1, 4, 1, 1,
		true, ui.AlignFill, false, ui.AlignFill)

	msggrid := ui.NewGrid()
	msggrid.SetPadded(true)
	grid.Append(msggrid,
		0, 5, 4, 1,
		false, ui.AlignCenter, false, ui.AlignStart)

	button = ui.NewButton("执行")
	button.OnClicked(func(*ui.Button) {
		csv1path := entry.Text()
		csv2path := entrydb.Text()
		target1 := entrylb11.Text()
		target2 := entrylb12.Text()
		fmt.Println("文件1路径" + entry.Text())
		fmt.Println("文件2路径" + entrydb.Text())
		fmt.Println("保存文件路径" + entry2.Text())
		fmt.Println("文件1列名" + entrylb11.Text())
		fmt.Println("文件2列名" + entrylb12.Text())
		if entry2.Text() != "" && entry2.Text() != "(cancelled)" {
			ccore.NewFileNameHb = entry2.Text()
		}
		ccore.Hb(csv1path, csv2path, target1, target2)
		ui.MsgBox(mainwin,
			"执行",
			"执行成功")
	})
	msggrid.Append(button,
		0, 0, 1, 1,
		false, ui.AlignFill, false, ui.AlignFill)
	return hbox
}

func setupUI() {
	mainwin = ui.NewWindow("csv 数据工具", 720, 480, true)
	mainwin.OnClosing(func(*ui.Window) bool {
		ui.Quit()
		return true
	})
	ui.OnShouldQuit(func() bool {
		mainwin.Destroy()
		return true
	})

	tab := ui.NewTab()
	mainwin.SetChild(tab)
	mainwin.SetMargined(true)

	tab.Append("筛选数据", makeSxCsv())
	tab.SetMargined(0, true)

	tab.Append("合并数据", makeHbCsv())
	tab.SetMargined(1, true)

	tab.Append("数据预览", makePreLook())
	tab.SetMargined(2, true)

	mainwin.Show()
}

func makePreLook() ui.Control {
	hbox := ui.NewHorizontalBox()
	hbox.SetPadded(true)

	vbox := ui.NewVerticalBox()
	vbox.SetPadded(true)
	hbox.Append(vbox, true)

	grid := ui.NewGrid()
	grid.SetPadded(true)
	vbox.Append(grid, false)

	button := ui.NewButton("打开文件")
	entry := ui.NewEntry()
	entry.SetReadOnly(true)
	grid.Append(button,
		0, 0, 1, 1,
		false, ui.AlignFill, false, ui.AlignFill)
	grid.Append(entry,
		1, 0, 1, 1,
		true, ui.AlignFill, false, ui.AlignFill)

	cbtn := ui.NewButton("清空内容")

	grid2 := ui.NewGrid()
	grid2.SetPadded(true)
	vbox.Append(grid2, false)
	grid2.Append(cbtn,
		0, 0, 1, 1,
		true, ui.AlignStart, false, ui.AlignFill)

	messageGroup := ui.NewGroup("预览数据")
	messageGroup.SetMargined(true)
	vbox.Append(messageGroup, false)
	vbMessage := ui.NewVerticalBox()
	vbMessage.SetPadded(true)
	//预览内容的容器
	//messageLabel := ui.NewLabel("")
	//vbMessage.Append(messageLabel, false)
	messageGroup.SetChild(vbMessage)
	var isflush bool = true
	//清空内容
	cbtn.OnClicked(func(button *ui.Button) {
		if !isflush {
			for i := 0; i < 10; i++ {
				vbMessage.Delete(0)
			}
			isflush = true
		}
	})

	button.OnClicked(func(*ui.Button) {
		if !isflush {
			for i := 0; i < 10; i++ {
				vbMessage.Delete(0)
			}
			isflush = true
		}
		filename := ui.OpenFile(mainwin)
		if filename == "" {
			filename = "(cancelled)"
		}
		entry.SetText(filename)
		//读取前10行的内容
		cutil.ReadLine(filename, func(bytes []byte) {
			res := string(bytes) + "\n"
			messageLabel := ui.NewLabel(res)
			vbMessage.Append(messageLabel, false)
		}, 10)
		isflush = false
	})

	return hbox
}

func main() {
	ui.Main(setupUI)
}
